<?php 
class ControllerCommonPaymentOnlineLogged extends Controller {
	private $error = array();
	
	
  	public function index() {
		$this->session->data['payment_invoice_type'] = 1; 
		
		
		$this->language->load('account/register');
    	$this->document->setTitle('Thông tin mua hàng');
		
		$this->load->model('catalog/information');
		$this->data['error'] = false;
		
		$info_end = $this->model_catalog_information->getInformation(55);
		$this->data['info_end'] = html_entity_decode($info_end['description'], ENT_QUOTES, 'UTF-8');
		
				
		if ((!$this->cart->hasProducts() && (!isset($this->session->data['vouchers']) || !$this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
			$this->redirect($this->url->link('checkout/cart'));
		}	
		
		
		$info_payment_online = $this->model_catalog_information->getInformation(59);
		$this->data['info_payment_online'] = html_entity_decode($info_payment_online['description'], ENT_QUOTES, 'UTF-8');
		
      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
        	'separator' => false
      	); 

    	$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['action'] = $this->url->link('common/payment_online_logged', '', 'SSL');
		$this->load->model('account/address');
		//$this->data['addresses'] = $this->model_account_address->getAddresses();
		//$this->data['addresse'] = $this->data['addresses'][0];
		if (isset($this->session->data['payment_address_id'])) {
				$this->data['address_id'] = $this->session->data['payment_address_id'];
			} else {
				$this->data['address_id'] = $this->customer->getAddressId();
			}
				
		//$this->data['addresses'] = $this->model_account_address->getAddresses();
			
		$this->data['addresse'] = $this->model_account_address->getAddress($this->data['address_id']);
		
		//print_r($this->data['address_id']);	
		//print_r($this->data['addresse']);	
		//exit();
		$this->data['isshipping'] = false;
		$this->data['common_confirm'] =false;
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
				//print_r($this->request->post);
				//	for(echo )
				//exit();
				if(($this->request->post['isshipping']==1) && $this->validate()) {
					$this->session->data['payment_address_id']  = $this->data['address_id'];
					$this->session->data['shipping_address_id'] = $this->model_account_address->addAnotherAddress($this->request->post);
					//print_r($this->session->data['payment_address_id']);
					//echo '------';
					//print_r($this->session->data['shipping_address_id']);
					$this->data['isshipping'] = true;
					$this->data['common_confirm'] = true;
					$this->initSesstionPaymentShipping();
					//$this->redirect($this->url->link('common/confirm'));
				} else 
				{
					if($this->request->post['isshipping']==0) {
						$this->session->data['payment_address_id'] = $this->data['address_id'];
						$this->session->data['shipping_address_id'] = $this->data['address_id'];
						//$this->redirect($this->url->link('common/confirm'));
						
						$this->data['common_confirm'] = true;
						$this->initSesstionPaymentShipping();
					}
				}
				//$this->redirect($this->url->link('common/confirm'));
				//$this->session->data['shipping_address_id'] = $this->model_account_address->addAnotherAddress($this->request->post);
		}
		


			$this->load->model('tool/image');
			
      		$this->data['products'] = array();

			$products = $this->cart->getProducts();

      		foreach ($products as $product) {
				$product_total = 0;
					
				foreach ($products as $product_2) {
					if ($product_2['product_id'] == $product['product_id']) {
						$product_total += $product_2['quantity'];
					}
				}			
				
				if ($product['minimum'] > $product_total) {
					$this->data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);
				}				
					
				if ($product['image']) {
					$image = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height'));
				} else {
					$image = '';
				}

				$option_data = array();

        		foreach ($product['option'] as $option) {
					if ($option['type'] != 'file') {
						$option_data[] = array(
							'name'  => $option['name'],
							'value' => (strlen($option['option_value']) > 20 ? substr($option['option_value'], 0, 20) . '..' : $option['option_value'])
						);
					} else {
						$this->load->library('encryption');
						
						$encryption = new Encryption($this->config->get('config_encryption'));
						
						$file = substr($encryption->decrypt($option['option_value']), 0, strrpos($encryption->decrypt($option['option_value']), '.'));
						
						$option_data[] = array(
							'name'  => $option['name'],
							'value' => (strlen($file) > 20 ? substr($file, 0, 20) . '..' : $file)
						);						
					}
        		}
				
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}

				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$total = $this->currency->format($this->tax->calculate($product['total'], $product['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$total = false;
				}
				
        		$this->data['products'][] = array(
          			'key'      => $product['key'],
          			'thumb'    => $image,
					'name'     => $product['name'],
          			'model'    => $product['model'],
          			'option'   => $option_data,
          			'quantity' => $product['quantity'],
          			'stock'    => $product['stock'],
					'reward'   => ($product['reward'] ? sprintf($this->language->get('text_reward'), $product['reward']) : ''),
					'price'    => $price,
					'total'    => $total,
					'href'     => $this->url->link('product/product', 'product_id=' . $product['product_id'])
        		);
      		}
		
		
		if (isset($this->error['shipping_firstname'])) {
			$this->data['error_shipping_firstname'] = $this->error['shipping_firstname'];
		} else {
			$this->data['error_shipping_firstname'] = '';
		}	
		
		if (isset($this->error['shipping_lastname'])) {
			$this->data['error_shipping_lastname'] = $this->error['shipping_lastname'];
		} else {
			$this->data['error_shipping_lastname'] = '';
		}		
	
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}
		
		if (isset($this->error['username'])) {
			$this->data['error_username'] = $this->error['username'];
		} else {
			$this->data['error_username'] = '';
		}
		
		if (isset($this->error['error_shipping_telephone'])) {
			$this->data['error_shipping_telephone'] = $this->error['error_shipping_telephone'];
		} else {
			$this->data['error_shipping_telephone'] = '';
		}
		
		if (isset($this->error['password'])) {
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}
		
 		if (isset($this->error['confirm'])) {
			$this->data['error_confirm'] = $this->error['confirm'];
		} else {
			$this->data['error_confirm'] = '';
		}
		
  		if (isset($this->error['shipping_address_1'])) {
			$this->data['error_shipping_address_1'] = $this->error['shipping_address_1'];
		} else {
			$this->data['error_shipping_address_1'] = '';
		}
		
		if (isset($this->error['shipping_address_2'])) {
			$this->data['error_shipping_address_2'] = $this->error['shipping_address_2'];
		} else {
			$this->data['error_shipping_address_2'] = '';
		}
   		
		if (isset($this->error['shipping_city'])) {
			$this->data['error_shipping_city'] = $this->error['shipping_city'];
		} else {
			$this->data['error_shipping_city'] = '';
		}
		
		if (isset($this->error['shipping_postcode'])) {
			$this->data['error_shipping_postcode'] = $this->error['shipping_postcode'];
		} else {
			$this->data['error_shipping_postcode'] = '';
		}

		if (isset($this->error['country'])) {
			$this->data['error_country'] = $this->error['country'];
		} else {
			$this->data['error_country'] = '';
		}

		if (isset($this->error['zone'])) {
			$this->data['error_zone'] = $this->error['zone'];
		} else {
			$this->data['error_zone'] = '';
		}
		
    	
		if (isset($this->request->post['username'])) {
    		$this->data['username'] = $this->request->post['username'];
		} else {
			$this->data['username'] = '';
		}
		
		if (isset($this->request->post['shipping_firstname'])) {
    		$this->data['shipping_firstname'] = $this->request->post['shipping_firstname'];
		} else {
			$this->data['shipping_firstname'] = '';
		}

		if (isset($this->request->post['shipping_lastname'])) {
    		$this->data['shipping_lastname'] = $this->request->post['shipping_lastname'];
		} else {
			$this->data['shipping_lastname'] = '';
		}
		
		if (isset($this->request->post['email'])) {
    		$this->data['email'] = $this->request->post['email'];
		} else {
			$this->data['email'] = '';
		}
		
		if (isset($this->request->post['shipping_telephone'])) {
    		$this->data['shipping_telephone'] = $this->request->post['shipping_telephone'];
		} else {
			
			$this->data['shipping_telephone'] = '';
		}
		if (isset($this->request->post['fax'])) {
    		$this->data['fax'] = $this->request->post['fax'];
		} else {
			$this->data['fax'] = '';
		}
		
		if (isset($this->request->post['company'])) {
    		$this->data['company'] = $this->request->post['company'];
		} else {
			$this->data['company'] = '';
		}
		
		if (isset($this->request->post['shipping_address_1'])) {
    		$this->data['shipping_address_1'] = $this->request->post['shipping_address_1'];
		} else {
			$this->data['shipping_address_1'] = '';
		}

		if (isset($this->request->post['shipping_address_2'])) {
    		$this->data['shipping_address_2'] = $this->request->post['shipping_address_2'];
		} else {
			$this->data['shipping_address_2'] = '';
		}

		if (isset($this->request->post['shipping_postcode'])) {
    		$this->data['shipping_postcode'] = $this->request->post['shipping_postcode'];
		} else {
			$this->data['shipping_postcode'] = '';
		}
		
		if (isset($this->request->post['shipping_city'])) {
    		$this->data['shipping_city'] = $this->request->post['shipping_city'];
		} else {
			$this->data['shipping_city'] = '';
		}

    	if (isset($this->request->post['shipping_country_id'])) {
      		$this->data['shipping_country_id'] = $this->request->post['shipping_country_id'];
		} else {	
      		$this->data['shipping_country_id'] = $this->config->get('config_shipping_country_id');
    	}

    	if (isset($this->request->post['shipping_zone_id'])) {
      		$this->data['shipping_zone_id'] = $this->request->post['shipping_zone_id']; 	
		} else {
      		$this->data['shipping_zone_id'] = '';
    	}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/payment_online_logged.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/payment_online_logged.tpl';
		} else {
			$this->template = 'default/template/common/payment_online_logged.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
				
		$this->response->setOutput($this->render());	
  	}
	private function validate() {
		
    	if ((utf8_strlen($this->request->post['shipping_firstname']) < 1) || (utf8_strlen($this->request->post['shipping_firstname']) > 32)) {
      		$this->error['shipping_firstname'] = $this->language->get('error_shipping_firstname');
    	}

    	if ((utf8_strlen($this->request->post['shipping_lastname']) < 1) || (utf8_strlen($this->request->post['shipping_lastname']) > 32)) {
      		$this->error['shipping_lastname'] = $this->language->get('error_shipping_lastname');
    	}

    	
    	if ((utf8_strlen($this->request->post['shipping_telephone']) < 1) || (utf8_strlen($this->request->post['shipping_telephone']) > 32)) {
      		$this->error['error_shipping_telephone'] = $this->language->get('error_shipping_telephone');
    	}

    	if ((utf8_strlen($this->request->post['shipping_address_1']) < 1) || (utf8_strlen($this->request->post['shipping_address_1']) > 128)) {
      		$this->error['shipping_address_1'] = $this->language->get('error_shipping_address_1');
    	}
		if ((utf8_strlen($this->request->post['shipping_address_2']) < 1) || (utf8_strlen($this->request->post['shipping_address_2']) > 128)) {
      		$this->error['shipping_address_2'] = $this->language->get('error_shipping_address_2');
    	}

    	if ((utf8_strlen($this->request->post['shipping_city']) < 1) || (utf8_strlen($this->request->post['shipping_city']) > 128)) {
      		$this->error['shipping_city'] = $this->language->get('error_shipping_city');
    	}

		$this->load->model('localisation/country');
		
		$country_info = $this->model_localisation_country->getCountry($this->request->post['shipping_country_id']);
		
		if ($country_info && $country_info['postcode_required'] && (utf8_strlen($this->request->post['shipping_postcode']) < 1) || (utf8_strlen($this->request->post['shipping_postcode']) > 20)) {
			$this->error['shipping_postcode'] = $this->language->get('error_shipping_postcode');
		}

    	if ($this->request->post['shipping_country_id'] == '') {
      		$this->error['country'] = $this->language->get('error_country');
    	}
		
    	if ($this->request->post['shipping_zone_id'] == '') {
      		$this->error['zone'] = $this->language->get('error_zone');
    	}

    	

		/*
		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info && !isset($this->request->post['agree'])) {
      			$this->error['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
			}
		}*/
		
    	if (!$this->error) {
			$this->data['error'] = false;
      		return true;
    	} else {
			$this->data['error'] = true;
      		return false;
    	}
  	}
	public function initSesstionPaymentShipping()
	{
			$this->load->model('account/address');
				
			if ($this->customer->isLogged()) {					
				$shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);		
			} elseif (isset($this->session->data['guest'])) {
				$shipping_address = $this->session->data['guest']['shipping'];
			}	
			// shipping methods
			
			//if (!isset($this->session->data['shipping_methods'])) {
					$quote_data = array();
					
					$this->load->model('setting/extension');
					
					$results = $this->model_setting_extension->getExtensions('shipping');
					
					foreach ($results as $result) {
						if ($this->config->get($result['code'] . '_status')) {
							$this->load->model('shipping/' . $result['code']);
							
							$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address); 
				
							if ($quote) {
								$quote_data[$result['code']] = array( 
									'title'      => $quote['title'],
									'quote'      => $quote['quote'], 
									'sort_order' => $quote['sort_order'],
									'error'      => $quote['error']
								);
							}
						}
					}
			
					$sort_order = array();
				  
					foreach ($quote_data as $key => $value) {
						$sort_order[$key] = $value['sort_order'];
					}
			
					array_multisort($sort_order, SORT_ASC, $quote_data);
					
					$this->session->data['shipping_methods'] = $quote_data;
					//print_r($this->session->data['shipping_methods']);
					/*if (isset($this->session->data['shipping_methods']['flat']['quote']['flat'])) {		
						$this->session->data['shipping_method'] = $this->session->data['shipping_methods']['flat']['quote']['flat'];
					} 
					*/
					if (isset($this->session->data['shipping_methods']['free']['quote']['free'])) {		
						$this->session->data['shipping_method'] = $this->session->data['shipping_methods']['free']['quote']['free'];
					} 
				//}
			
			// end shipping methods
			$total_data = array();					
			$total = 0;
			$taxes = $this->cart->getTaxes();
			
			$this->load->model('setting/extension');
			
			$sort_order = array(); 
			
			$results = $this->model_setting_extension->getExtensions('total');
			
			foreach ($results as $key => $value) {
				$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
			}
			
			array_multisort($sort_order, SORT_ASC, $results);
			
			foreach ($results as $result) {
				if ($this->config->get($result['code'] . '_status')) {
					$this->load->model('total/' . $result['code']);
		
					$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
				}
			}
			// Payment Methods
			$method_data = array();
			
			$this->load->model('setting/extension');
			if ($this->customer->isLogged()) {
				$payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);		
			} elseif (isset($this->session->data['guest'])) {
				$payment_address = $this->session->data['guest']['payment'];
			}	
			$results = $this->model_setting_extension->getExtensions('payment');
			
			foreach ($results as $result) {
				if ($this->config->get($result['code'] . '_status')) {
					$this->load->model('payment/' . $result['code']);
					
					$method = $this->{'model_payment_' . $result['code']}->getMethod($payment_address, $total); 
					
					if ($method) {
						$method_data[$result['code']] = $method;
					}
				}
			}
			$this->session->data['payment_methods'] = $method_data;
			//$this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];
			//print_r($this->session->data['payment_methods']);
			if(isset($this->session->data['payment_methods']) && isset($this->session->data['payment_methods']['free_checkout'])) {
				$this->session->data['payment_method'] = $this->session->data['payment_methods']['free_checkout'];
			} 
			//end dunglt
	}
}
?>